home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / doc / interpreter / expr.tex < prev    next >
Text File  |  1997-08-13  |  36KB  |  1,129 lines

  1. @c Copyright (C) 1996, 1997 John W. Eaton
  2. @c This is part of the Octave manual.
  3. @c For copying conditions, see the file gpl.tex.
  4.  
  5. @node Expressions, Evaluation, Variables, Top
  6. @chapter Expressions
  7. @cindex expressions
  8.  
  9. Expressions are the basic building block of statements in Octave.  An
  10. expression evaluates to a value, which you can print, test, store in a
  11. variable, pass to a function, or assign a new value to a variable with
  12. an assignment operator.
  13.  
  14. An expression can serve as a statement on its own.  Most other kinds of
  15. statements contain one or more expressions which specify data to be
  16. operated on.  As in other languages, expressions in Octave include
  17. variables, array references, constants, and function calls, as well as
  18. combinations of these with various operators.
  19.  
  20. @menu
  21. * Index Expressions::           
  22. * Calling Functions::           
  23. * Arithmetic Ops::              
  24. * Comparison Ops::              
  25. * Boolean Expressions::         
  26. * Assignment Ops::              
  27. * Increment Ops::               
  28. * Operator Precedence::         
  29. @end menu
  30.  
  31. @node Index Expressions, Calling Functions, Expressions, Expressions
  32. @section Index Expressions
  33.  
  34. @opindex (
  35. @opindex )
  36.  
  37. An @dfn{index expression} allows you to reference or extract selected
  38. elements of a matrix or vector.
  39.  
  40. Indices may be scalars, vectors, ranges, or the special operator
  41. @samp{:}, which may be used to select entire rows or columns.
  42.  
  43. Vectors are indexed using a single expression.  Matrices require two
  44. indices unless the value of the built-in variable
  45. @code{do_fortran_indexing} is nonzero, in which case matrices may
  46. also be indexed by a single expression.
  47.  
  48. @defvr {Built-in Variable} do_fortran_indexing
  49. If the value of @code{do_fortran_indexing} is nonzero, Octave allows 
  50. you to select elements of a two-dimensional matrix using a single index
  51. by treating the matrix as a single vector created from the columns of
  52. the matrix.  The default value is 0. 
  53. @end defvr
  54.  
  55. Given the matrix
  56.  
  57. @example
  58. a = [1, 2; 3, 4]
  59. @end example
  60.  
  61. @noindent
  62. all of the following expressions are equivalent
  63.  
  64. @example
  65. @group
  66. a (1, [1, 2])
  67. a (1, 1:2)
  68. a (1, :)
  69. @end group
  70. @end example
  71.  
  72. @noindent
  73. and select the first row of the matrix.
  74.  
  75. A special form of indexing may be used to select elements of a matrix or
  76. vector.  If the indices are vectors made up of only ones and zeros, the
  77. result is a new matrix whose elements correspond to the elements of the
  78. index vector that are equal to one.  For example,
  79.  
  80. @example
  81. @group
  82. a = [1, 2; 3, 4];
  83. a ([1, 0], :)
  84. @end group
  85. @end example
  86.  
  87. @noindent
  88. selects the first row of the matrix @code{a}.
  89.  
  90. This operation can be useful for selecting elements of a matrix based on
  91. some condition, since the comparison operators return matrices of ones
  92. and zeros.
  93.  
  94. This special zero-one form of indexing leads to a conflict with the
  95. standard indexing operation.  For example, should the following
  96. statements
  97.  
  98. @example
  99. @group
  100. a = [1, 2; 3, 4];
  101. a ([1, 1], :)
  102. @end group
  103. @end example
  104.  
  105. @noindent
  106. return the original matrix, or the matrix formed by selecting the first
  107. row twice?  Although this conflict is not likely to arise very often in
  108. practice, you may select the behavior you prefer by setting the built-in
  109. variable @code{prefer_zero_one_indexing}.
  110.  
  111. @defvr {Built-in Variable} prefer_zero_one_indexing
  112. If the value of @code{prefer_zero_one_indexing} is nonzero, Octave
  113. will perform zero-one style indexing when there is a conflict with the
  114. normal indexing rules.  @xref{Index Expressions}.  For example, given a
  115. matrix
  116.  
  117. @example
  118. a = [1, 2, 3, 4]
  119. @end example
  120.  
  121. @noindent
  122. with @code{prefer_zero_one_indexing} is set to nonzero, the
  123. expression
  124.  
  125. @example
  126. a ([1, 1, 1, 1])
  127. @end example
  128.  
  129. @noindent
  130. results in the matrix @code{[ 1, 2, 3, 4 ]}.  If the value of
  131. @code{prefer_zero_one_indexing} set to 0, the result would be
  132. the matrix @code{[ 1, 1, 1, 1 ]}.
  133.  
  134. In the first case, Octave is selecting each element corresponding to a
  135. @samp{1} in the index vector.  In the second, Octave is selecting the
  136. first element multiple times.
  137.  
  138. The default value for @code{prefer_zero_one_indexing} is 0.
  139. @end defvr
  140.  
  141. Finally, indexing a scalar with a vector of ones can be used to create a
  142. vector the same size as the the index vector, with each element equal to
  143. the value of the original scalar.  For example, the following statements
  144.  
  145. @example
  146. @group
  147. a = 13;
  148. a ([1, 1, 1, 1])
  149. @end group
  150. @end example
  151.  
  152. @noindent
  153. produce a vector whose four elements are all equal to 13.
  154.  
  155. Similarly, indexing a scalar with two vectors of ones can be used to
  156. create a matrix.  For example the following statements
  157.  
  158. @example
  159. @group
  160. a = 13;
  161. a ([1, 1], [1, 1, 1])
  162. @end group
  163. @end example
  164.  
  165. @noindent
  166. create a 2 by 3 matrix with all elements equal to 13.
  167.  
  168. This is an obscure notation and should be avoided.  It is better to
  169. use the function @code{ones} to generate a matrix of the appropriate
  170. size whose elements are all one, and then to scale it to produce the
  171. desired result.  @xref{Special Utility Matrices}.
  172.  
  173. @defvr {Built-in Variable} prefer_column_vectors
  174. If @code{prefer_column_vectors} is nonzero, operations like
  175.  
  176. @example
  177. for i = 1:10
  178.   a (i) = i;
  179. endfor
  180. @end example
  181.  
  182. @noindent
  183. (for @code{a} previously  undefined) produce column vectors.  Otherwise, row
  184. vectors are preferred.  The default value is 0.
  185.  
  186. If a variable is already defined to be a vector (a matrix with a single
  187. row or column), the original orientation is respected, regardless of the
  188. value of @code{prefer_column_vectors}.
  189. @end defvr
  190.  
  191. @defvr {Built-in Variable} resize_on_range_error
  192. If the value of @code{resize_on_range_error} is nonzero, expressions
  193. like
  194.  
  195. @example
  196. for i = 1:10
  197.   a (i) = sqrt (i);
  198. endfor
  199. @end example
  200.  
  201. @noindent
  202. (for @code{a} previously undefined) result in the variable @code{a}
  203. being resized to be just large enough to hold the new value.  New
  204. elements that have not been given a value are set to zero.  If the value
  205. of @code{resize_on_range_error} is 0, an error message is printed and
  206. control is returned to the top level.  The default value is 1.
  207. @end defvr
  208.  
  209. Note that it is quite inefficient to create a vector using a loop like
  210. the one shown in the example above.  In this particular case, it would
  211. have been much more efficient to use the expression
  212.  
  213. @example
  214. a = sqrt (1:10);
  215. @end example
  216.  
  217. @noindent
  218. thus avoiding the loop entirely.  In cases where a loop is still
  219. required, or a number of values must be combined to form a larger
  220. matrix, it is generally much faster to set the size of the matrix first,
  221. and then insert elements using indexing commands.  For example, given a
  222. matrix @code{a},
  223.  
  224. @example
  225. @group
  226. [nr, nc] = size (a);
  227. x = zeros (nr, n * nc);
  228. for i = 1:n
  229.   x(:,(i-1)*n+1:i*n) = a;
  230. endfor
  231. @end group
  232. @end example
  233.  
  234. @noindent
  235. is considerably faster than
  236.  
  237. @example
  238. @group
  239. x = a;
  240. for i = 1:n-1
  241.   x = [x, a];
  242. endfor
  243. @end group
  244. @end example
  245.  
  246. @noindent
  247. particularly for large matrices because Octave does not have to
  248. repeatedly resize the result.
  249.  
  250. @node Calling Functions, Arithmetic Ops, Index Expressions, Expressions
  251. @section Calling Functions
  252.  
  253. A @dfn{function} is a name for a particular calculation.  Because it has
  254. a name, you can ask for it by name at any point in the program.  For
  255. example, the function @code{sqrt} computes the square root of a number.
  256.  
  257. A fixed set of functions are @dfn{built-in}, which means they are
  258. available in every Octave program.  The @code{sqrt} function is one of
  259. these.  In addition, you can define your own functions.
  260. @xref{Functions and Scripts}, for information about how to do this.
  261.  
  262. @cindex arguments in function call
  263. The way to use a function is with a @dfn{function call} expression,
  264. which consists of the function name followed by a list of
  265. @dfn{arguments} in parentheses. The arguments are expressions which give
  266. the raw materials for the calculation that the function will do.  When
  267. there is more than one argument, they are separated by commas.  If there
  268. are no arguments, you can omit the parentheses, but it is a good idea to
  269. include them anyway, to clearly indicate that a function call was
  270. intended.  Here are some examples:
  271.  
  272. @example
  273. @group
  274. sqrt (x^2 + y^2)      # @r{One argument}
  275. ones (n, m)           # @r{Two arguments}
  276. rand ()               # @r{No arguments}
  277. @end group
  278. @end example
  279.  
  280. Each function expects a particular number of arguments.  For example, the
  281. @code{sqrt} function must be called with a single argument, the number
  282. to take the square root of:
  283.  
  284. @example
  285. sqrt (@var{argument})
  286. @end example
  287.  
  288. Some of the built-in functions take a variable number of arguments,
  289. depending on the particular usage, and their behavior is different
  290. depending on the number of arguments supplied.
  291.  
  292. Like every other expression, the function call has a value, which is
  293. computed by the function based on the arguments you give it.  In this
  294. example, the value of @code{sqrt (@var{argument})} is the square root of
  295. the argument.  A function can also have side effects, such as assigning
  296. the values of certain variables or doing input or output operations.
  297.  
  298. Unlike most languages, functions in Octave may return multiple values.
  299. For example, the following statement
  300.  
  301. @example
  302. [u, s, v] = svd (a)
  303. @end example
  304.  
  305. @noindent
  306. computes the singular value decomposition of the matrix @code{a} and
  307. assigns the three result matrices to @code{u}, @code{s}, and @code{v}.
  308.  
  309. The left side of a multiple assignment expression is itself a list of
  310. expressions, and is allowed to be a list of variable names or index
  311. expressions.  See also @ref{Index Expressions}, and @ref{Assignment Ops}.
  312.  
  313. @menu
  314. * Call by Value::               
  315. * Recursion::                   
  316. @end menu
  317.  
  318. @node Call by Value, Recursion, Calling Functions, Calling Functions
  319. @subsection Call by Value
  320.  
  321. In Octave, unlike Fortran, function arguments are passed by value, which
  322. means that each argument in a function call is evaluated and assigned to
  323. a temporary location in memory before being passed to the function.
  324. There is currently no way to specify that a function parameter should be
  325. passed by reference instead of by value.  This means that it is
  326. impossible to directly alter the value of function parameter in the
  327. calling function.  It can only change the local copy within the function
  328. body.  For example, the function
  329.  
  330. @example
  331. @group
  332. function f (x, n)
  333.   while (n-- > 0)
  334.     disp (x);
  335.   endwhile
  336. endfunction
  337. @end group
  338. @end example
  339.  
  340. @noindent
  341. displays the value of the first argument @var{n} times.  In this
  342. function, the variable @var{n} is used as a temporary variable without
  343. having to worry that its value might also change in the calling
  344. function.  Call by value is also useful because it is always possible to
  345. pass constants for any function parameter without first having to
  346. determine that the function will not attempt to modify the parameter.
  347.  
  348. The caller may use a variable as the expression for the argument, but
  349. the called function does not know this: it only knows what value the
  350. argument had.  For example, given a function called as
  351.  
  352. @example
  353. @group
  354. foo = "bar";
  355. fcn (foo)
  356. @end group
  357. @end example
  358.  
  359. @noindent
  360. you should not think of the argument as being ``the variable
  361. @code{foo}.''  Instead, think of the argument as the string value,
  362. @code{"bar"}.
  363.  
  364. Even though Octave uses pass-by-value semantics for function arguments,
  365. values are not copied unnecessarily.  For example,
  366.  
  367. @example
  368. @group
  369. x = rand (1000);
  370. f (x);
  371. @end group
  372. @end example
  373.  
  374. @noindent
  375. does not actually force two 1000 by 1000 element matrices to exist
  376. @emph{unless} the function @code{f} modifies the value of its
  377. argument.  Then Octave must create a copy to avoid changing the
  378. value outside the scope of the function @code{f}, or attempting (and
  379. probably failing!) to modify the value of a constant or the value of a
  380. temporary result.
  381.  
  382. @node Recursion,  , Call by Value, Calling Functions
  383. @subsection Recursion
  384. @cindex factorial function
  385.  
  386. With some restrictions@footnote{Some of Octave's function are
  387. implemented in terms of functions that cannot be called recursively.
  388. For example, the ODE solver @code{lsode} is ultimately implemented in a
  389. Fortran subroutine that cannot be called recursively, so @code{lsode}
  390. should not be called either directly or indirectly from within the
  391. user-supplied function that @code{lsode} requires.  Doing so will result
  392. in undefined behavior.}, recursive function calls are allowed.  A
  393. @dfn{recursive function} is one which calls itself, either directly or
  394. indirectly.  For example, here is an inefficient@footnote{It would be
  395. much better to use @code{prod (1:n)}, or @code{gamma (n+1)} instead,
  396. after first checking to ensure that the value @code{n} is actually a
  397. positive integer.} way to compute the factorial of a given integer:
  398.  
  399. @example
  400. @group
  401. function retval = fact (n)
  402.   if (n > 0)
  403.     retval = n * fact (n-1);
  404.   else
  405.     retval = 1;
  406.   endif
  407. endfunction
  408. @end group
  409. @end example
  410.  
  411. This function is recursive because it calls itself directly.  It
  412. eventually terminates because each time it calls itself, it uses an
  413. argument that is one less than was used for the previous call.  Once the
  414. argument is no longer greater than zero, it does not call itself, and
  415. the recursion ends.
  416.  
  417. There is currently no limit on the recursion depth, so infinite
  418. recursion is possible.  If this happens, Octave will consume more and
  419. more memory attempting to store intermediate values for each function
  420. call context until there are no more resources available.  This is
  421. obviously undesirable, and will probably be fixed in some future version
  422. of Octave by allowing users to specify a maximum allowable recursion
  423. depth.
  424.  
  425. @node Arithmetic Ops, Comparison Ops, Calling Functions, Expressions
  426. @section Arithmetic Operators
  427. @cindex arithmetic operators
  428. @cindex operators, arithmetic
  429. @cindex addition
  430. @cindex subtraction
  431. @cindex multiplication
  432. @cindex matrix multiplication
  433. @cindex division
  434. @cindex quotient
  435. @cindex negation
  436. @cindex unary minus
  437. @cindex exponentiation
  438. @cindex transpose
  439. @cindex Hermitian operator
  440. @cindex transpose, complex-conjugate
  441. @cindex complex-conjugate transpose
  442.  
  443. The following arithmetic operators are available, and work on scalars
  444. and matrices.
  445.  
  446. @table @code
  447. @item @var{x} + @var{y}
  448. @opindex +
  449. Addition.  If both operands are matrices, the number of rows and columns
  450. must both agree.  If one operand is a scalar, its value is added to
  451. all the elements of the other operand.
  452.  
  453. @item @var{x} .+ @var{y}
  454. @opindex .+
  455. Element by element addition.  This operator is equivalent to @code{+}.
  456.  
  457. @item @var{x} - @var{y}
  458. @opindex -
  459. Subtraction.  If both operands are matrices, the number of rows and
  460. columns of both must agree.
  461.  
  462. @item @var{x} .- @var{y}
  463. Element by element subtraction.  This operator is equivalent to @code{-}.
  464.  
  465. @item @var{x} * @var{y}
  466. @opindex *
  467. Matrix multiplication.  The number of columns of @var{x} must agree
  468. with the number of rows of @var{y}.
  469.  
  470. @item @var{x} .* @var{y}
  471. @opindex .*
  472. Element by element multiplication.  If both operands are matrices, the
  473. number of rows and columns must both agree.
  474.  
  475. @item @var{x} / @var{y}
  476. @opindex /
  477. Right division.  This is conceptually equivalent to the expression
  478.  
  479. @example
  480. (inverse (y') * x')'
  481. @end example
  482.  
  483. @noindent
  484. but it is computed without forming the inverse of @var{y'}.
  485.  
  486. If the system is not square, or if the coefficient matrix is singular,
  487. a minimum norm solution is computed.
  488.  
  489. @item @var{x} ./ @var{y}
  490. @opindex ./
  491. Element by element right division.
  492.  
  493. @item @var{x} \ @var{y}
  494. @opindex \
  495. Left division.  This is conceptually equivalent to the expression
  496.  
  497. @example
  498. inverse (x) * y
  499. @end example
  500.  
  501. @noindent
  502. but it is computed without forming the inverse of @var{x}.
  503.  
  504. If the system is not square, or if the coefficient matrix is singular,
  505. a minimum norm solution is computed.
  506.  
  507. @item @var{x} .\ @var{y}
  508. @opindex .\
  509. Element by element left division.  Each element of @var{y} is divided
  510. by each corresponding element of @var{x}.
  511.  
  512. @item @var{x} ^ @var{y}
  513. @itemx @var{x} ** @var{y}
  514. @opindex **
  515. @opindex ^
  516. Power operator.  If @var{x} and @var{y} are both scalars, this operator
  517. returns @var{x} raised to the power @var{y}.  If @var{x} is a scalar and
  518. @var{y} is a square matrix, the result is computed using an eigenvalue
  519. expansion.  If @var{x} is a square matrix. the result is computed by
  520. repeated multiplication if @var{y} is an integer, and by an eigenvalue
  521. expansion if @var{y} is not an integer.  An error results if both
  522. @var{x} and @var{y} are matrices.
  523.  
  524. The implementation of this operator needs to be improved.
  525.  
  526. @item @var{x} .^ @var{y}
  527. @item @var{x} .** @var{y}
  528. @opindex .**
  529. @opindex .^
  530. Element by element power operator.  If both operands are matrices, the
  531. number of rows and columns must both agree.
  532.  
  533. @item -@var{x}
  534. @opindex -
  535. Negation.
  536.  
  537. @item +@var{x}
  538. @opindex +
  539. Unary plus.  This operator has no effect on the operand.
  540.  
  541. @item @var{x}'
  542. @opindex '
  543. Complex conjugate transpose.  For real arguments, this operator is the
  544. same as the transpose operator.  For complex arguments, this operator is
  545. equivalent to the expression
  546.  
  547. @example
  548. conj (x.')
  549. @end example
  550.  
  551. @item @var{x}.'
  552. @opindex .'
  553. Transpose.
  554. @end table
  555.  
  556. Note that because Octave's element by element operators begin with a
  557. @samp{.}, there is a possible ambiguity for statements like
  558.  
  559. @example
  560. 1./m
  561. @end example
  562.  
  563. @noindent
  564. because the period could be interpreted either as part of the constant
  565. or as part of the operator.  To resolve this conflict, Octave treats the
  566. expression as if you had typed
  567.  
  568. @example
  569. (1) ./ m
  570. @end example
  571.  
  572. @noindent
  573. and not
  574.  
  575. @example
  576. (1.) / m
  577. @end example
  578.  
  579. @noindent
  580. Although this is inconsistent with the normal behavior of Octave's
  581. lexer, which usually prefers to break the input into tokens by
  582. preferring the longest possible match at any given point, it is more
  583. useful in this case.
  584.  
  585. @defvr {Built-in Variable} warn_divide_by_zero
  586. If the value of @code{warn_divide_by_zero} is nonzero, a warning
  587. is issued when Octave encounters a division by zero.  If the value is
  588. 0, the warning is omitted.  The default value is 1.
  589. @end defvr
  590.  
  591. @node Comparison Ops, Boolean Expressions, Arithmetic Ops, Expressions
  592. @section Comparison Operators
  593. @cindex comparison expressions
  594. @cindex expressions, comparison
  595. @cindex relational operators
  596. @cindex operators, relational
  597. @cindex less than operator
  598. @cindex greater than operator
  599. @cindex equality operator
  600. @cindex tests for equality
  601. @cindex equality, tests for
  602.  
  603. @dfn{Comparison operators} compare numeric values for relationships
  604. such as equality.  They are written using
  605. @emph{relational operators}.
  606.  
  607. All of Octave's comparison operators return a value of 1 if the
  608. comparison is true, or 0 if it is false.  For matrix values, they all
  609. work on an element-by-element basis.  For example,
  610.  
  611. @example
  612. @group
  613. [1, 2; 3, 4] == [1, 3; 2, 4]
  614.      @result{}  1  0
  615.          0  1
  616. @end group
  617. @end example
  618.  
  619. If one operand is a scalar and the other is a matrix, the scalar is
  620. compared to each element of the matrix in turn, and the result is the
  621. same size as the matrix.
  622.  
  623. @table @code
  624. @item @var{x} < @var{y}
  625. @opindex <
  626. True if @var{x} is less than @var{y}.
  627.  
  628. @item @var{x} <= @var{y}
  629. @opindex <=
  630. True if @var{x} is less than or equal to @var{y}.
  631.  
  632. @item @var{x} == @var{y}
  633. @opindex ==
  634. True if @var{x} is equal to @var{y}.
  635.  
  636. @item @var{x} >= @var{y}
  637. @opindex >=
  638. True if @var{x} is greater than or equal to @var{y}.
  639.  
  640. @item @var{x} > @var{y}
  641. @opindex >
  642. True if @var{x} is greater than @var{y}.
  643.  
  644. @item @var{x} != @var{y}
  645. @itemx @var{x} ~= @var{y}
  646. @itemx @var{x} <> @var{y}
  647. @opindex !=
  648. @opindex ~=
  649. @opindex <>
  650. True if @var{x} is not equal to @var{y}.
  651. @end table
  652.  
  653. String comparisons may also be performed with the @code{strcmp}
  654. function, not with the comparison operators listed above.
  655. @xref{Strings}.
  656.  
  657. @node Boolean Expressions, Assignment Ops, Comparison Ops, Expressions
  658. @section Boolean Expressions
  659. @cindex expressions, boolean
  660. @cindex boolean expressions
  661. @cindex expressions, logical
  662. @cindex logical expressions
  663. @cindex operators, boolean
  664. @cindex boolean operators
  665. @cindex logical operators
  666. @cindex operators, logical
  667. @cindex and operator
  668. @cindex or operator
  669. @cindex not operator
  670.  
  671. @menu
  672. * Element-by-element Boolean Operators::  
  673. * Short-circuit Boolean Operators::  
  674. @end menu
  675.  
  676. @node Element-by-element Boolean Operators, Short-circuit Boolean Operators, Boolean Expressions, Boolean Expressions
  677. @subsection Element-by-element Boolean Operators
  678. @cindex element-by-element evaluation
  679.  
  680. An @dfn{element-by-element boolean expression} is a combination of
  681. comparison expressions using the boolean
  682. operators ``or'' (@samp{|}), ``and'' (@samp{&}), and ``not'' (@samp{!}),
  683. along with parentheses to control nesting.  The truth of the boolean
  684. expression is computed by combining the truth values of the
  685. corresponding elements of the component expressions.  A value is
  686. considered to be false if it is zero, and true otherwise.
  687.  
  688. Element-by-element boolean expressions can be used wherever comparison
  689. expressions can be used.  They can be used in @code{if} and @code{while}
  690. statements.  However, if a matrix value used as the condition in an
  691. @code{if} or @code{while} statement is only true if @emph{all} of its
  692. elements are nonzero.
  693.  
  694. Like comparison operations, each element of an element-by-element
  695. boolean expression also has a numeric value (1 if true, 0 if false) that
  696. comes into play if the result of the boolean expression is stored in a
  697. variable, or used in arithmetic.
  698.  
  699. Here are descriptions of the three element-by-element boolean operators.
  700.  
  701. @table @code
  702. @item @var{boolean1} & @var{boolean2}
  703. @opindex &
  704. Elements of the result are true if both corresponding elements of
  705. @var{boolean1} and @var{boolean2} are true.
  706.  
  707. @item @var{boolean1} | @var{boolean2}
  708. @opindex |
  709. Elements of the result are true if either of the corresponding elements
  710. of @var{boolean1} or @var{boolean2} is true.
  711.  
  712. @item ! @var{boolean}
  713. @itemx ~ @var{boolean}
  714. @opindex ~
  715. @opindex !
  716. Each element of the result is true if the corresponding element of
  717. @var{boolean} is false.
  718. @end table
  719.  
  720. For matrix operands, these operators work on an element-by-element
  721. basis.  For example, the expression
  722.  
  723. @example
  724. [1, 0; 0, 1] & [1, 0; 2, 3]
  725. @end example
  726.  
  727. @noindent
  728. returns a two by two identity matrix.
  729.  
  730. For the binary operators, the dimensions of the operands must conform if
  731. both are matrices.  If one of the operands is a scalar and the other a
  732. matrix, the operator is applied to the scalar and each element of the
  733. matrix.
  734.  
  735. For the binary element-by-element boolean operators, both subexpressions
  736. @var{boolean1} and @var{boolean2} are evaluated before computing the
  737. result.  This can make a difference when the expressions have side
  738. effects.  For example, in the expression
  739.  
  740. @example
  741. a & b++
  742. @end example
  743.  
  744. @noindent
  745. the value of the variable @var{b} is incremented even if the variable
  746. @var{a} is zero.
  747.  
  748. This behavior is necessary for the boolean operators to work as
  749. described for matrix-valued operands.
  750.  
  751. @node Short-circuit Boolean Operators,  , Element-by-element Boolean Operators, Boolean Expressions
  752. @subsection Short-circuit Boolean Operators
  753. @cindex short-circuit evaluation
  754.  
  755. Combined with the implicit conversion to scalar values in @code{if} and
  756. @code{while} conditions, Octave's element-by-element boolean operators
  757. are often sufficient for performing most logical operations.  However,
  758. it is sometimes desirable to stop evaluating a boolean expression as
  759. soon as the overall truth value can be determined.  Octave's
  760. @dfn{short-circuit} boolean operators work this way.
  761.  
  762. @table @code
  763. @item @var{boolean1} && @var{boolean2}
  764. @opindex &&
  765. The expression @var{boolean1} is evaluated and converted to a scalar
  766. using the equivalent of the operation @code{all (all (@var{boolean1}))}.
  767. If it is false, the result of the overall expression is 0.  If it is
  768. true, the expression @var{boolean2} is evaluated and converted to a
  769. scalar using the equivalent of the operation @code{all (all
  770. (@var{boolean1}))}.  If it is true, the result of the overall expression
  771. is 1.  Otherwise, the result of the overall expression is 0.
  772.  
  773. @item @var{boolean1} || @var{boolean2}
  774. @opindex ||
  775. The expression @var{boolean1} is evaluated and converted to a scalar
  776. using the equivalent of the operation @code{all (all (@var{boolean1}))}.
  777. If it is true, the result of the overall expression is 1.  If it is
  778. false, the expression @var{boolean2} is evaluated and converted to a
  779. scalar using the equivalent of the operation @code{all (all
  780. (@var{boolean1}))}.  If it is true, the result of the overall expression
  781. is 1.  Otherwise, the result of the overall expression is 0.
  782. @end table
  783.  
  784. The fact that both operands may not be evaluated before determining the
  785. overall truth value of the expression can be important.  For example, in
  786. the expression
  787.  
  788. @example
  789. a && b++
  790. @end example
  791.  
  792. @noindent
  793. the value of the variable @var{b} is only incremented if the variable
  794. @var{a} is nonzero.
  795.  
  796. This can be used to write somewhat more concise code.  For example, it
  797. is possible write
  798.  
  799. @example
  800. @group
  801. function f (a, b, c)
  802.   if (nargin > 2 && isstr (c))
  803.     @dots{}
  804. @end group
  805. @end example
  806.  
  807. @noindent
  808. instead of having to use two @code{if} statements to avoid attempting to
  809. evaluate an argument that doesn't exist.  For example, without the
  810. short-circuit feature, it would be necessary to write
  811.  
  812. @example
  813. @group
  814. function f (a, b, c)
  815.   if (nargin > 2)
  816.     if (isstr (c))
  817.       @dots{}
  818. @end group
  819. @end example
  820.  
  821. Writing
  822.  
  823. @example
  824. @group
  825. function f (a, b, c)
  826.   if (nargin > 2 & isstr (c))
  827.     @dots{}
  828. @end group
  829. @end example
  830.  
  831. @noindent
  832. would result in an error if @code{f} were called with one or two
  833. arguments because Octave would be forced to try to evaluate both of the
  834. operands for the operator @samp{&}.
  835.  
  836. @node Assignment Ops, Increment Ops, Boolean Expressions, Expressions
  837. @section Assignment Expressions
  838. @cindex assignment expressions
  839. @cindex assignment operators
  840. @cindex operators, assignment
  841. @cindex expressions, assignment
  842.  
  843. @opindex =
  844.  
  845. An @dfn{assignment} is an expression that stores a new value into a
  846. variable.  For example, the following expression assigns the value 1 to
  847. the variable @code{z}:
  848.  
  849. @example
  850. z = 1
  851. @end example
  852.  
  853. After this expression is executed, the variable @code{z} has the value 1.
  854. Whatever old value @code{z} had before the assignment is forgotten.
  855. The @samp{=} sign is called an @dfn{assignment operator}.
  856.  
  857. Assignments can store string values also.  For example, the following
  858. expression would store the value @code{"this food is good"} in the
  859. variable @code{message}:
  860.  
  861. @example
  862. @group
  863. thing = "food"
  864. predicate = "good"
  865. message = [ "this " , thing , " is " , predicate ]
  866. @end group
  867. @end example
  868.  
  869. @noindent
  870. (This also illustrates concatenation of strings.)
  871.  
  872. @cindex side effect
  873. Most operators (addition, concatenation, and so on) have no effect
  874. except to compute a value.  If you ignore the value, you might as well
  875. not use the operator.  An assignment operator is different.  It does
  876. produce a value, but even if you ignore the value, the assignment still
  877. makes itself felt through the alteration of the variable.  We call this
  878. a @dfn{side effect}.
  879.  
  880. @cindex lvalue
  881. The left-hand operand of an assignment need not be a variable
  882. (@pxref{Variables}).  It can also be an element of a matrix
  883. (@pxref{Index Expressions}) or a list of return values
  884. (@pxref{Calling Functions}).  These are all called @dfn{lvalues}, which
  885. means they can appear on the left-hand side of an assignment operator.
  886. The right-hand operand may be any expression.  It produces the new value
  887. which the assignment stores in the specified variable, matrix element,
  888. or list of return values.
  889.  
  890. It is important to note that variables do @emph{not} have permanent types.
  891. The type of a variable is simply the type of whatever value it happens
  892. to hold at the moment.  In the following program fragment, the variable
  893. @code{foo} has a numeric value at first, and a string value later on:
  894.  
  895. @example
  896. @group
  897. octave:13> foo = 1
  898. foo = 1
  899. octave:13> foo = "bar"
  900. foo = bar
  901. @end group
  902. @end example
  903.  
  904. @noindent
  905. When the second assignment gives @code{foo} a string value, the fact that
  906. it previously had a numeric value is forgotten.
  907.  
  908. Assignment of a scalar to an indexed matrix sets all of the elements
  909. that are referenced by the indices to the scalar value.  For example, if
  910. @code{a} is a matrix with at least two columns,
  911.  
  912. @example
  913. @group
  914. a(:, 2) = 5
  915. @end group
  916. @end example
  917.  
  918. @noindent
  919. sets all the elements in the second column of @code{a} to 5.
  920.  
  921. Assigning an empty matrix @samp{[]} works in most cases to allow you to
  922. delete rows or columns of matrices and vectors.  @xref{Empty Matrices}.
  923. For example, given a 4 by 5 matrix @var{A}, the assignment
  924.  
  925. @example
  926. A (3, :) = []
  927. @end example
  928.  
  929. @noindent
  930. deletes the third row of @var{A}, and the assignment
  931.  
  932. @example
  933. A (:, 1:2:5) = []
  934. @end example
  935.  
  936. @noindent
  937. deletes the first, third, and fifth columns.
  938.  
  939. An assignment is an expression, so it has a value.  Thus, @code{z = 1}
  940. as an expression has the value 1.  One consequence of this is that you
  941. can write multiple assignments together:
  942.  
  943. @example
  944. x = y = z = 0
  945. @end example
  946.  
  947. @noindent
  948. stores the value 0 in all three variables.  It does this because the
  949. value of @code{z = 0}, which is 0, is stored into @code{y}, and then
  950. the value of @code{y = z = 0}, which is 0, is stored into @code{x}.
  951.  
  952. This is also true of assignments to lists of values, so the following is
  953. a valid expression
  954.  
  955. @example
  956. [a, b, c] = [u, s, v] = svd (a)
  957. @end example
  958.  
  959. @noindent
  960. that is exactly equivalent to
  961.  
  962. @example
  963. @group
  964. [u, s, v] = svd (a)
  965. a = u
  966. b = s
  967. c = v
  968. @end group
  969. @end example
  970.  
  971. In expressions like this, the number of values in each part of the
  972. expression need not match.  For example, the expression
  973.  
  974. @example
  975. [a, b, c, d] = [u, s, v] = svd (a)
  976. @end example
  977.  
  978. @noindent
  979. is equivalent to the expression above, except that the value of the
  980. variable @samp{d} is left unchanged, and the expression
  981.  
  982. @example
  983. [a, b] = [u, s, v] = svd (a)
  984. @end example
  985.  
  986. @noindent
  987. is equivalent to 
  988.  
  989. @example
  990. @group
  991. [u, s, v] = svd (a)
  992. a = u
  993. b = s
  994. @end group
  995. @end example
  996.  
  997. You can use an assignment anywhere an expression is called for.  For
  998. example, it is valid to write @code{x != (y = 1)} to set @code{y} to 1
  999. and then test whether @code{x} equals 1.  But this style tends to make
  1000. programs hard to read.  Except in a one-shot program, you should rewrite
  1001. it to get rid of such nesting of assignments.  This is never very hard.
  1002.  
  1003. @cindex increment operator
  1004. @cindex decrement operator
  1005. @cindex operators, increment
  1006. @cindex operators, decrement
  1007.  
  1008. @node Increment Ops, Operator Precedence, Assignment Ops, Expressions
  1009. @section Increment Operators
  1010.  
  1011. @emph{Increment operators} increase or decrease the value of a variable
  1012. by 1.  The operator to increment a variable is written as @samp{++}.  It
  1013. may be used to increment a variable either before or after taking its
  1014. value.
  1015.  
  1016. For example, to pre-increment the variable @var{x}, you would write
  1017. @code{++@var{x}}.  This would add one to @var{x} and then return the new
  1018. value of @var{x} as the result of the expression.  It is exactly the
  1019. same as the expression @code{@var{x} = @var{x} + 1}.
  1020.  
  1021. To post-increment a variable @var{x}, you would write @code{@var{x}++}.
  1022. This adds one to the variable @var{x}, but returns the value that
  1023. @var{x} had prior to incrementing it.  For example, if @var{x} is equal
  1024. to 2, the result of the expression @code{@var{x}++} is 2, and the new
  1025. value of @var{x} is 3.
  1026.  
  1027. For matrix and vector arguments, the increment and decrement operators
  1028. work on each element of the operand.
  1029.  
  1030. Here is a list of all the increment and decrement expressions.
  1031.  
  1032. @table @code
  1033. @item ++@var{x}
  1034. @opindex ++
  1035. This expression increments the variable @var{x}.  The value of the
  1036. expression is the @emph{new} value of @var{x}.  It is equivalent to the
  1037. expression @code{@var{x} = @var{x} + 1}.
  1038.  
  1039. @item --@var{x}
  1040. @opindex @code{--}
  1041. This expression decrements the variable @var{x}.  The value of the
  1042. expression is the @emph{new} value of @var{x}.  It is equivalent to the
  1043. expression @code{@var{x} = @var{x} - 1}.
  1044.  
  1045. @item @var{x}++
  1046. @opindex ++
  1047. This expression causes the variable @var{x} to be incremented.  The
  1048. value of the expression is the @emph{old} value of @var{x}.
  1049.  
  1050. @item @var{x}--
  1051. @opindex @code{--}
  1052. This expression causes the variable @var{x} to be decremented.  The
  1053. value of the expression is the @emph{old} value of @var{x}.
  1054. @end table
  1055.  
  1056. It is not currently possible to increment index expressions.  For
  1057. example, you might expect that the expression @code{@var{v}(4)++} would
  1058. increment the fourth element of the vector @var{v}, but instead it
  1059. results in a parse error.  This problem may be fixed in a future
  1060. release of Octave.
  1061.  
  1062. @node Operator Precedence,  , Increment Ops, Expressions
  1063. @section Operator Precedence
  1064. @cindex operator precedence
  1065.  
  1066. @dfn{Operator precedence} determines how operators are grouped, when
  1067. different operators appear close by in one expression.  For example,
  1068. @samp{*} has higher precedence than @samp{+}.  Thus, the expression
  1069. @code{a + b * c} means to multiply @code{b} and @code{c}, and then add
  1070. @code{a} to the product (i.e., @code{a + (b * c)}).
  1071.  
  1072. You can overrule the precedence of the operators by using parentheses.
  1073. You can think of the precedence rules as saying where the parentheses
  1074. are assumed if you do not write parentheses yourself.  In fact, it is
  1075. wise to use parentheses whenever you have an unusual combination of
  1076. operators, because other people who read the program may not remember
  1077. what the precedence is in this case.  You might forget as well, and then
  1078. you too could make a mistake.  Explicit parentheses will help prevent
  1079. any such mistake.
  1080.  
  1081. When operators of equal precedence are used together, the leftmost
  1082. operator groups first, except for the assignment and exponentiation
  1083. operators, which group in the opposite order.  Thus, the expression
  1084. @code{a - b + c} groups as @code{(a - b) + c}, but the expression
  1085. @code{a = b = c} groups as @code{a = (b = c)}.
  1086.  
  1087. The precedence of prefix unary operators is important when another
  1088. operator follows the operand.  For example, @code{-x^2} means
  1089. @code{-(x^2)}, because @samp{-} has lower precedence than @samp{^}.
  1090.  
  1091. Here is a table of the operators in Octave, in order of increasing
  1092. precedence.
  1093.  
  1094. @table @code
  1095. @item statement separators
  1096. @samp{;}, @samp{,}.
  1097.  
  1098. @item assignment
  1099. @samp{=}.  This operator groups right to left.
  1100.  
  1101. @item logical "or" and "and"
  1102. @samp{||}, @samp{&&}.
  1103.  
  1104. @item element-wise "or" and "and"
  1105. @samp{|}, @samp{&}.
  1106.  
  1107. @item relational
  1108. @samp{<}, @samp{<=}, @samp{==}, @samp{>=}, @samp{>}, @samp{!=},
  1109. @samp{~=}, @samp{<>}.
  1110.  
  1111. @item colon
  1112. @samp{:}.
  1113.  
  1114. @item add, subtract
  1115. @samp{+}, @samp{-}.
  1116.  
  1117. @item multiply, divide
  1118. @samp{*}, @samp{/}, @samp{\}, @samp{.\}, @samp{.*}, @samp{./}.
  1119.  
  1120. @item transpose
  1121. @samp{'}, @samp{.'}
  1122.  
  1123. @item unary plus, minus, increment, decrement, and ``not''
  1124. @samp{+}, @samp{-}, @samp{++}, @samp{--}, @samp{!}, @samp{~}.
  1125.  
  1126. @item exponentiation
  1127. @samp{^}, @samp{**}, @samp{.^}, @samp{.**}.
  1128. @end table
  1129.